package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.ExchangePrice;
import cc.yugu.yhdw.common.domain.hdw.ExchangePriceSetting;
import cc.yugu.yhdw.common.entity.json.DataResult;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangePriceMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangePriceSettingMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangePriceTimeMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/**
 * 换电价格
 * 
 * @Date:Created in 2018/7/9 14:00
 * @Author:
 * @Modified By: zjn 2018/7/9 14:00
 */ 
@Service
public class ExchangePriceService extends AbstractService {

    @Autowired
    ExchangePriceMapper exchangePriceMapper;
    @Autowired
    ExchangePriceSettingMapper exchangePriceSettingMapper;
    @Autowired
    ExchangePriceTimeMapper exchangePriceTimeMapper;
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/9  14:00
    */ 
    public Page findPage(ExchangePrice search) {
        Page page = search.buildPage();

        page.setTotalItems(exchangePriceMapper.findPageCounts(search));
        search.setBeginIndex(page.getOffset());
        List<ExchangePrice> list = exchangePriceMapper.findPageResults(search);
//        for (ExchangePrice exchangePrice: list) {
//            exchangePrice.setAgentName(findAgentInfo(exchangePrice.getAgentId()).getAgentName());
//        }
        page.setResult(list);
        return page;
    }

    public ExchangePrice find(Long id) {
        return exchangePriceMapper.find(id);
    }
    /**
    * 创建新建套餐种类（无代理商）
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.DataResult
    * @Author By: zjn 2018/7/9  14:01
    */ 
    public DataResult create(ExchangePrice entity) {
        entity.setCreateTime(new Date());
        int total = exchangePriceMapper.insert(entity);
        if (total == 0) {
            return DataResult.failResult("创建失败","");
        }
        return DataResult.successResult(entity.getId());
    }
    /**
    * 带有代理商的家畜套餐信息插入
    *
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.DataResult
    * @Author By: zjn 2018/7/31  13:26
    */
    public DataResult createAgencyId(ExchangePrice entity) {
        entity.setCreateTime(new Date());
        int total = exchangePriceMapper.insertAgencyId(entity);
        if (total == 0) {
            return DataResult.failResult("创建失败","");
        }
        return DataResult.successResult(entity.getId());
    }
    /**
    * 更新
    *
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  14:01
    */
    public ExtResult update(ExchangePrice entity) {
        if(exchangePriceMapper.update(entity)  == 0){
            return ExtResult.failResult("操作失败！");
        }
        return ExtResult.successResult();
    }
    public ExtResult updateAgencyId(ExchangePrice entity) {
//        ExchangePriceSetting exchangePriceSetting = new ExchangePriceSetting();
        if(exchangePriceMapper.updateAgencyId(entity)  == 0){
            return ExtResult.failResult("操作失败！");
        }
       //exchangePriceSetting.setBasePriceId(entity.getType());
        // 隐形bug ，套餐修改类型之后，点击修改，弹出页面，还显示原来的类型名称
//        if(entity.getType()==1){
//            exchangePriceSetting.setBasePriceId(entity.getId());
//            exchangePriceSetting.setActivityPriceId(null);
//            exchangePriceSettingMapper.updateBasePriceId(exchangePriceSetting);
//        }
//        if(entity.getType()==2){
//            exchangePriceSetting.setActivityPriceId(entity.getId());
//            exchangePriceSetting.setBasePriceId(null);
//            exchangePriceSettingMapper.updateActivityPriceId(exchangePriceSetting);
//        }
        return ExtResult.successResult();
    }
    /**
    * 删除
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  14:03
    */ 
    public ExtResult delete(Long id) {
        List<ExchangePriceSetting> list = exchangePriceSettingMapper.findByPriceIdList(id);
        List<ExchangePriceSetting> activeList = exchangePriceSettingMapper.findByActivityIdList(id);
        if (list.size() > 0 || activeList.size() > 0) {
            return ExtResult.failResult("此套餐已被使用, 不能删除！");
        }
        exchangePriceTimeMapper.deleteByPriceId(id);
        int total = exchangePriceMapper.delete(id);
        if (total == 0) {
            return ExtResult.failResult("操作失败！");
        }
        return ExtResult.successResult();
    }
}
